#ifndef ALSA
#define ALSA

#include <QThread>
#include <QUdpSocket>
#include <QSemaphore>

#include "tinyalsa.h"

#define ALSA_RUNNING 3
#define ALSA_STOPPED 2
#define ALSA_PLAY 1
#define ALSA_STOP 0

class AlsaRecv : public QThread
{
    Q_OBJECT

public:
    AlsaRecv():
        status(ALSA_STOP),
        running(false),
        pcm_lock(1)
    {
    }

    void run();

    void stop();

    void set_status(unsigned char status);

    unsigned char get_status();

    void set_alsa_dev(struct pcm* pcm);

    bool is_running();

public slots:
    void audio_data_received();

public:
    ~AlsaRecv()
    {
        if(this->audio_recv)
        {
            delete this->audio_recv;
            this->audio_recv = NULL;
        }
    }

private:
    QUdpSocket  *audio_recv;
    unsigned char status;
    struct pcm* pcm;
    bool running;

    int audio_pipe_fd;

    QSemaphore pcm_lock;
};

class Alsa : public QThread
{
public:
    Alsa():
        pcm_cap(NULL),
        status(ALSA_STOP),
        running(false),
        pcm_lock(1),
        pcm_cap_lock(1)
    {
        alsa_recv = new AlsaRecv();
    }

    void run();

    void change_status();

    unsigned char get_status();

    unsigned char get_recv_status();

    bool is_running();

    void set_status(unsigned char status);

    void capture();

    void stop();

    void receive();

public:
    ~Alsa()
    {
        if(this->pcm_cap)
            pcm_dev_close(pcm_cap);
        if(this->alsa_recv)
        {
            delete this->alsa_recv;
            this->alsa_recv = NULL;
        }
        if(this->audio_send)
        {
            delete this->audio_send;
            this->audio_send = NULL;
        }
    }

private:
    struct pcm* pcm_cap;
    struct pcm* pcm;

    QUdpSocket  *audio_send;

    AlsaRecv* alsa_recv;

    unsigned char status;
    bool running;
    QSemaphore pcm_lock;
    QSemaphore pcm_cap_lock;
};

class AlsaSim : public QThread
{
    Q_OBJECT
public:
    AlsaSim():
        pcm_lock(1)
    {

    }

    void run();

    void stop();

    void set_status(unsigned char status);

    ~AlsaSim()
    {

    }

private:
    struct pcm* pcm_cap;
    struct pcm* pcm;
    unsigned char status;

    QSemaphore pcm_lock;

};



#endif // ALSA

